<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Library\Logic\ShopUserMessageLogic;
use App\Models\ShopCustomerImModel;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ImController extends AdminController
{
    protected $title = '聊天';

    /**
     * grid
     * @return NGrid
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    protected function grid(): NGrid
    {
        $receive = (int)request()->get('receive', 0);
        $send = (int)request()->get('send', 0);
        if ($receive <= 0 || $send <= 0) die;
        $grid = new NGrid(new ShopCustomerImModel);
        $grid->model()->where(function (Builder $builder) use ($send, $receive) {
            $builder->where(function (Builder $builder) use ($send, $receive) {
                $builder->where('send_user_id', $send)->where('receive_user_id', $receive);
            })->orWhere(function (Builder $builder) use ($send, $receive) {
                $builder->where('send_user_id', $receive)->where('receive_user_id', $send);
            });
        })->orderByDesc('ci_id');
        $grid->column('ci_id', 'ID');
        $grid->column('send_user_id', '发送人');
        $grid->column('receive_user_id', '接受人');
        $grid->column('ci_receive_read', '是否已读')->using([1 => '已读', 2 => '未读']);
        $grid->column('ci_content', '内容');
        $grid->column('ci_content_img', '图片')->display(function ($column) {
            if (!is_null($column)) return view('image', ['id' => $this->ci_id, 'image' => fileIdToUrl($column)]);
            else return null;
        });
        $grid->column('add_at', __('l.createdAt'));
        return $grid;
    }
}
